﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace wfaFoxMigrator.Extensions
{
    public static class StringExtensions
    {
        /// <summary>
        /// Determines whether [contains] [the specified STR].
        /// </summary>
        /// <param name="str">The STR.</param>
        /// <param name="collection">The collection.</param>
        /// <returns>
        /// 	<c>true</c> if [contains] [the specified value]; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsContainedIn<T>(this string str, List<T> collection)
        {
            return collection.Where(ob => ob.ToString().Equals(str)).Count() > 0;
        }
        /// <summary>
        /// Determines whether [is contained in] [the specified string value].
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="stringValue">The string value.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="collection">The collection.</param>
        /// <returns>
        /// 	<c>true</c> if [is contained in] [the specified string value]; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsContainedIn<T>(this string stringValue, string propertyName, List<T> collection)
        {
            ParameterExpression param = Expression.Parameter(typeof(string), "o");
            MemberExpression propOBID = Expression.PropertyOrField(param, propertyName);
            Expression expOBID = Expression.Equal(propOBID, Expression.Constant(stringValue));
            var lambda = Expression.Lambda(expOBID, param);
            MethodCallExpression whereExpression = Expression.Call(typeof(Queryable), "Where", new Type[] { typeof(string) }, expOBID, lambda);

            return collection.Where(ob => ob.ToString().Equals(stringValue)).Count() > 0;
        }
    }    
}